package org.example.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.netty.handler.logging.ByteBufFormat;
import io.vertx.core.Vertx;
import io.vertx.core.net.NetServer;
import io.vertx.core.net.NetServerOptions;
import org.example.domain.Sell;
import org.example.domain.TakePosition;
import org.example.domain.TradeOrder;
import org.example.domain.User;
import org.example.service.SellService;
import org.example.service.TakePositionService;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class TCPService {
    @Autowired
    private UserService userService;
    @Autowired
    private TakePositionService takePositionService;
    @Autowired
    private SellService sellService;
    @PostConstruct
    public void init() {
        Vertx vertx = Vertx.vertx();
        NetServerOptions options = new NetServerOptions()
                .setLogActivity(true)
                .setActivityLogDataFormat(ByteBufFormat.SIMPLE);
        NetServer server = vertx.createNetServer(options);
        server.connectHandler(socket -> {
            socket.handler(buffer -> {
//                System.out.println("Received data: " + buffer.toString());
                int anInt = buffer.getInt(0);
                System.err.println(anInt);
                String string = buffer.getString(4,4+anInt);
//                String string = buffer.toString();
                TradeOrder tradeOrder = JSONUtil.toBean(string, TradeOrder.class);
                System.out.println("============>撮合完的数据"+tradeOrder);
                priceAndNum(tradeOrder);



            });


        });
        server.listen(8896, "127.0.0.1", res -> {
            if (res.succeeded()) {
                System.out.println("监听成功");
            } else {
                System.out.println("监听失败");
            }
        });
    }
    //撮合完成修改用户的金额，修改持仓的数量，
    public void priceAndNum(TradeOrder tradeOrder){

        User buyUser = userService.getById(tradeOrder.getBuyId());
        buyUser.setMoney(buyUser.getMoney().subtract(tradeOrder.getPrice()));
        boolean b = userService.updateById(buyUser);
        User sellUser = userService.getById(tradeOrder.getSellId());
        sellUser.setMoney(sellUser.getMoney().add(tradeOrder.getPrice()));
        boolean b1 = userService.updateById(sellUser);
        if (b&&b1){
            LambdaQueryWrapper<TakePosition> takePositionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            takePositionLambdaQueryWrapper.eq(TakePosition::getStockId,tradeOrder.getStockId());
            TakePosition takePosition = takePositionService.getOne(takePositionLambdaQueryWrapper);
            if(tradeOrder.getBuyNum()==0){
                takePosition.setUserId(tradeOrder.getBuyId());
            }
            takePosition.setNum(tradeOrder.getBuyNum());
            boolean b2 = takePositionService.updateById(takePosition);
            LambdaQueryWrapper<Sell> sellLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sellLambdaQueryWrapper.eq(Sell::getPositionId,takePosition.getId());
            Sell sell = sellService.getOne(sellLambdaQueryWrapper);
            sell.setNum(tradeOrder.getBuyNum());
            sellService.updateById(sell);
            System.out.println("撮合成功");
        }else {
            System.out.println("撮合失败");
        }
    }
}
